//
//  VisitorView.swift
//  SinaWeiBo
//
//  Created by Leon 笑 竹 on 2017/8/2.
//  Copyright © 2017年 Leon 笑 竹. All rights reserved.
//

import UIKit

class VisitorView: UIView {
    
    
    ///设置访客视图的字典
    var visitorInfo : [String : String]? {
        
        didSet {
            
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                    return
            }
            
            tipLabel.text = message
            
            //首页不需要设置(首页传空)
            if imageName == "" {
                
                //开始动画
                startAnimation()
                return
                
                //非主页
            }else{
                //设置图标
                iconImage.image = UIImage.init(named: imageName)
                //隐藏房子图标
                homeImage.isHidden = true
                //隐藏遮罩
                maskIcon.isHidden = true
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
     
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///旋转转轮视图
    fileprivate func startAnimation(){
        
        let rotateAni = CABasicAnimation(keyPath: "transform.rotation")
        rotateAni.toValue = -2 * Double.pi
        rotateAni.repeatCount = MAXFLOAT
        rotateAni.duration = 15
        //完成之后不删除(必须要记的属性)
        rotateAni.isRemovedOnCompletion = false
        
        iconImage.layer.add(rotateAni, forKey: nil)
    }
    
    
// MARK: - 设置私有属性
    
    /// 图像视图
   fileprivate lazy var iconImage = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_image_smallicon"))
    fileprivate lazy var maskIcon = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_mask_smallicon"))
    /// 房子视图
   fileprivate lazy var homeImage = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_image_house"))
    /// 提示标签
    fileprivate lazy var tipLabel : UILabel = UILabel.cz_label(
        
                withText: "关注一些人，回头看看有什么惊喜",
                fontSize: 14,
                color: UIColor.darkGray)
    
    /// 注册按钮
    lazy var registerBtn : UIButton = UIButton.cz_textButton(
                "注册",
                fontSize: 16,
                normalColor: UIColor.orange,
                highlightedColor: UIColor.black,
                backgroundImageName: "common_button_white_disable")

    /// 登录按钮
    
     lazy var loginBtn : UIButton = UIButton.cz_textButton(
                "登录",
                fontSize: 16,
                normalColor: UIColor.darkGray,
                highlightedColor: UIColor.black,
                backgroundImageName: "common_button_white_disable")
}


extension VisitorView {
    
    fileprivate func setupUI(){
        
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        
        /// 添加控件
        addSubview(iconImage)
        addSubview(maskIcon)
        addSubview(homeImage)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        /// 添加约束
        homeImage.snp.makeConstraints { (make) in
            make.top.equalTo(self).offset(200)
            make.centerX.equalTo(self)
        }
        
        iconImage.snp.makeConstraints { (make) in
            make.center.equalTo(homeImage)
            make.size.width.equalTo(200)
            make.size.height.equalTo(200)
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconImage.snp.bottom).offset(5)
            make.centerX.equalTo(self)
            make.height.equalTo(40)
            make.left.equalTo(self).offset(70)
            make.right.equalTo(self).offset(-70)
//            make.size.width.equalTo(100)
        }
        
        registerBtn.snp.makeConstraints { (make) in
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.leading.equalTo(tipLabel.snp.leading)
            make.width.equalTo(80)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.top.equalTo(registerBtn)
            make.trailing.equalTo(tipLabel.snp.trailing)
            make.width.equalTo(registerBtn)
        }
        
        //遮罩
        maskIcon.snp.makeConstraints { (make) in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(iconImage.snp.top)
            make.height.equalTo(200)
        }
    }
}
